<?php

/**
 * @file
 * Admin page callbacks for the search module.
 */

function cao_admin($callback_arg = '')
{
//    $op = isset($_POST['op']) ? $_POST['op'] : $callback_arg;
//            if (!empty($_POST['accounts']) && isset($_POST['operation']) && ($_POST['operation'] == 'cancel')) {
//                $build['user_multiple_cancel_confirm'] = drupal_get_form('user_multiple_cancel_confirm');
//            }
//            else {
//                $build['user_filter_form'] = drupal_get_form('user_filter_form');
    $build['cao_admin_slideshow'] = drupal_get_form('cao_admin_slideshow');
//            }
    return $build;
}

/**
 * Form builder; User administration page.
 *
 * @ingroup forms
 * @see user_admin_account_validate()
 * @see user_admin_account_submit()
 */
function cao_admin_slideshow() {

    $header = array(
        'image_name' => array('data' => t('Image Name'), 'field' => 's.image_name'),
        'thumbnail' => array('data' => t('Thumbnail'), 'field' => 's.thumbnail'),
        'sort_order' => array('data' => t('Sort Order'), 'field' => 's.sort_order', 'sort' => 'desc'),
        'operations' => array('data' => t('Operations')),
    );
//
//    $query = db_select('cao_slideshow', 's');
//    $query->condition('s.id', 0, '<>');
    $result = db_query("select * from cao_slideshow");
//    cao_build_filter_query($query);
//
//    $count_query = clone $query;
//    $count_query->addExpression('COUNT(u.uid)');
//
//    $query = $query->extend('PagerDefault')->extend('TableSort');
//    $query
//        ->fields('u', array('uid', 'name', 'status', 'created', 'access'))
//        ->limit(50)
//        ->orderByHeader($header)
//        ->setCountQuery($count_query);
//    $result = $query->execute();
//
//    $form['options'] = array(
//        '#type' => 'fieldset',
//        '#title' => t('Update options'),
//        '#attributes' => array('class' => array('container-inline')),
//    );
//    $options = array();
//    foreach (module_invoke_all('user_operations') as $operation => $array) {
//        $options[$operation] = $array['label'];
//    }
//    $form['options']['operation'] = array(
//        '#type' => 'select',
//        '#title' => t('Operation'),
//        '#title_display' => 'invisible',
//        '#options' => $options,
//        '#default_value' => 'unblock',
//    );
//    $options = array();
//    $form['options']['submit'] = array(
//        '#type' => 'submit',
//        '#value' => t('Update'),
//    );
//
    $destination = drupal_get_destination();
//
//    $status = array(t('blocked'), t('active'));
//    $roles = array_map('check_plain', user_roles(TRUE));
//    $accounts = array();
    foreach ($result as $account) {
//        $users_roles = array();
//        $roles_result = db_query('SELECT rid FROM {users_roles} WHERE uid = :uid', array(':uid' => $account->uid));
//        foreach ($roles_result as $user_role) {
//            $users_roles[] = $roles[$user_role->rid];
//        }
//        asort($users_roles);
//
        $options[$account->id] = array(
            'image_name' => $account->image_name,//theme('username', array('account' => $account)),
            'thumbnail' => $account->image_path, //$status[$account->status],
            'sort_order' => $account->sort_order,//theme('item_list', array('items' => $users_roles)),
//            'member_for' => format_interval(REQUEST_TIME - $account->created),
//            'access' =>  $account->access ? t('@time ago', array('@time' => format_interval(REQUEST_TIME - $account->access))) : t('never'),
            'operations' => array(
                'data' => array('#type' => 'link', '#title' => t('edit'),
                    '#href' => "admin/cao/$account->id/edit",
                    '#options' => array('query' => $destination)))
        );
    }
//
    $form['slideshow'] = array(
        '#type' => 'tableselect',
        '#header' => $header,
        '#options' => $options,
        '#empty' => t('No people available.'),
    );
//    $form['pager'] = array('#markup' => theme('pager'));

    return $form;
}




/**
 *
 */
function cao_admin_settings_submit($form, $form_state)
{

    $image_name = $form_state['values']['cao_node_types1'];
    $image_file = $form_state['values']['cao_node_types2'];
    if(!empty($image_name))
    {
        if(empty($image_file))
        form_set_error('cao_node_types1', t('please input image name'));
    }
//    foreach ($form_state['values']['cao_node_types'] as $key => $value) {
//    //gia tri cua form ma chung ta vua tao.
//        if (!$value) {
//    //tao them mot field du lieu vao database co ten cao
//            $instance = field_info_instance('node', 'cao', $key);
//            if (!empty($instance)) {
//    //neu ton tai thi delete field cũ đi
//                field_delete_instance($instance);
//                watchdog("Cao", 'Delete cao từ content type:
//    %key', array('%key' => $key));
//            }
//        } else {
//    //nếu không tìm thấy field nào trùng, chúng tao tạo mới field
//            $instance = field_info_instance('node', 'cao', $key);
//            if (empty($instance)) {
//                $instance = array(
//                    'field_name' => 'cao',
//                    'entity_type' => 'node',
//                    'bundle' => $key,
//                    'label' => t('Mo ta ngan'),
//                    'widget_type' => 'text_textarea_with_cao',
//                    'settings' => array('display_cao' => TRUE),
//                    'display' => array(
//                        'default' => array(
//                            'type' => 'text_default',
//                        ),
//                        'teaser' => array(
//                            'type' => 'text_cao_or_trimmed',
//                        ),
//                    ),
//                );
//                $instance = field_create_instance($instance);
//                watchdog('Cao', 'Thêm mới field Cao từ content type: %key',
//                    array('%key' => $key));
//            }
//        }
//    } // End foreach loop.
}
